package hasthi.actuators;

import static hasthi.common.HasthiOptions.SYSTEM_PORFILE;

import java.io.File;
import java.io.IOException;

import org.apache.xmlbeans.XmlException;

import hasthi.actuators.event.EventCorrelator;
import hasthi.common.BaseContext;
import hasthi.common.HasthiException;
import hasthi.common.SafeScheduler;
import hasthi.common.HasthiException.FaultCode;
import hasthi.config.SystemProfileDocument;
import hasthi.container.UnicastContianier;
import hasthi.container.p2p.MulticastContianier;
import hasthi.coordinator.CoordinatorContext;
import hasthi.decision.RuleEngine;
import hasthi.decision.planning.Planner;
import hasthi.decision.planning.SimplePlanner;
import hasthi.manager.ManagerContext;
import hasthi.metamodel.ResourceModel;

public class ActionContext {
    private ResourceModel resourceModel;
    private SystemConfig systemConfig;
    private UnicastContianier soapContainer;
    private MulticastContianier p2pContainer;
    private final BaseContext baseContext;
    private RemoteHostProxy remoteHostProxy;
    private Planner planner;
    private RuleEngine ruleEngine;
    private boolean isCoordinator = false;
    private CoordinatorContext coordinatorContext;
    private SafeScheduler scheduler;
    private EventCorrelator eventCorrelator;
    private ManagerContext managerContext;
    
    public ActionContext(BaseContext baseContext, boolean isCoordinator) throws HasthiException {
        try {
            this.baseContext = baseContext;
            String systemProfileFile = baseContext.getProperty(SYSTEM_PORFILE);
            if(systemProfileFile != null){
                SystemProfileDocument profileDocument = SystemProfileDocument.Factory.parse(new File(systemProfileFile));
                profileDocument.validate();
                systemConfig = new SystemConfig(profileDocument.getSystemProfile());
            }
            planner = new SimplePlanner();
            planner.init(this);
            eventCorrelator = new EventCorrelator();
            this.isCoordinator = isCoordinator;
        } catch (XmlException e) {
            throw new HasthiException(e,FaultCode.InitalizationError);
        } catch (IOException e) {
            throw new HasthiException(e,FaultCode.InitalizationError);
        }
    }
    
    public SystemConfig getSystemConfig() {
        return systemConfig;
    }

    public ResourceModel getResourceModel() {
        return resourceModel;
    }

    public void setResourceModel(ResourceModel resourceModel) {
        this.resourceModel = resourceModel;
    }

    public UnicastContianier getUnicastContainer() {
        return soapContainer;
    }

    public void setUnicastContainer(UnicastContianier soapContainer) {
        this.soapContainer = soapContainer;
    }

    public MulticastContianier getMulticastContainer() {
        return p2pContainer;
    }

    public void setMulticastContainer(MulticastContianier container) {
        p2pContainer = container;
    }

    public void setSystemConfig(SystemConfig systemConfig) {
        this.systemConfig = systemConfig;
    }

    public BaseContext getBaseContext() {
        return baseContext;
    }

    public Planner getPlanner() {
        return planner;
    }

    public RuleEngine getRuleEngine() {
        return ruleEngine;
    }

    public void setRuleEngine(RuleEngine ruleEngine) {
        this.ruleEngine = ruleEngine;
    }

    public RemoteHostProxy getRemoteHostProxy() {
        return remoteHostProxy;
    }

    public void setRemoteHostProxy(RemoteHostProxy remoteHostProxy) {
        this.remoteHostProxy = remoteHostProxy;
    }

    public boolean isCoordinator() {
        return isCoordinator;
    }

    public CoordinatorContext getCoordinatorContext() {
        return coordinatorContext;
    }

    public void setCoordinatorContext(CoordinatorContext coordinatorContext) {
        this.coordinatorContext = coordinatorContext;
        this.managerContext = coordinatorContext.getMyManagerContext();
    }

    public SafeScheduler getScheduler() {
        return scheduler;
    }

    public void setScheduler(SafeScheduler scheduler) {
        this.scheduler = scheduler;
    }

    public EventCorrelator getEventCorrelator() {
        return eventCorrelator;
    }

    public ManagerContext getManagerContext() {
        return managerContext;
    }

    public void setManagerContext(ManagerContext managerContext) {
        this.managerContext = managerContext;
    }
}
